package com.ptmall.app.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ptmall.app.R;
import com.ptmall.app.bean.CollectionBean;
import com.ptmall.app.bean.model.HomePageIndex;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.ui.activity.GoodDetilActivity;
import com.ptmall.app.utils.GlideUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2018/1/12.
 */

public class CollectionAdapter extends BaseAdapter {

    private Context mContext;
    private List<CollectionBean> mDataList;
    private LayoutInflater mLayoutInflater;

    public CollectionAdapter(Context context, List<CollectionBean> list) {
        this.mContext = context;
        this.mDataList = list;
        this.mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void pushData(List<CollectionBean> list){
        this.mDataList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(mDataList == null){
            return 0;
        }
        return mDataList.size();
    }


    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = mLayoutInflater.inflate(R.layout.item_colltiongood,  parent,false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        CollectionBean bean = mDataList.get(position);

        viewHolder.goodname.setText(bean.product_name);
        viewHolder.goodprice.setText("￥"+bean.sell_price);
        viewHolder.collectionnum.setText(bean.product_collect+"人已收藏");

        GlideUtil.showImg(mContext,bean.list_img,viewHolder.img);

        viewHolder.addcar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Map<String, String> map = new HashMap<>();
                map.put("productId", bean.productId);
                map.put("num", "1");
                ApiDataRepository.getInstance().addShopCar(map, new ApiNetResponse<Object>() {
                    @Override
                    public void onSuccess(Object data) {
                        Toast.makeText(mContext,"加入购物车成功",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

        viewHolder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, GoodDetilActivity.class);
                intent.putExtra("id",bean.productId);
                mContext.startActivity(intent);
            }
        });

        return convertView;
    }

    class ViewHolder {

        TextView goodprice,goodname,collectionnum,gooddesc;
        ImageView img,addcar;

        ViewHolder(View view) {
            img = (ImageView) view.findViewById(R.id.goodimg);
            addcar = (ImageView) view.findViewById(R.id.addcar);
            gooddesc = (TextView) view.findViewById(R.id.gooddesc);
            collectionnum = (TextView) view.findViewById(R.id.collectionnum);
            goodname = (TextView) view.findViewById(R.id.goodname);
            goodprice = (TextView) view.findViewById(R.id.goodprice);
        }
    }

}
